package com.qf.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dao.EmployeeDao;
import com.qf.entity.Employee;
import com.qf.exception.CustomerException;
import com.qf.param.EmployeeAddOrUpdateParam;
import com.qf.service.EmployeeService;
import com.qf.utils.PageParam;
import com.qf.utils.PageVO;
import com.qf.vo.EmployeeListVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author zdl
 * @date 2025/3/14
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Resource
    private EmployeeDao employeeDao;

    @Override
    public PageVO<Employee> getEmployeeList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<Employee> employeeListVOS = employeeDao.selectEmployeeList();
        PageInfo<Employee> employeeListVOPageInfo = new PageInfo<>(employeeListVOS);
        if (employeeListVOPageInfo != null) {
            return new PageVO<>(employeeListVOPageInfo.getTotal(), employeeListVOPageInfo.getList());
        }
        return null;
    }

    @Override
    public int addEmployee(EmployeeAddOrUpdateParam employeeAddParam) {
        if (employeeAddParam.getTime() == null) {
            employeeAddParam.setTime(new Date());
        }
        employeeAddParam.setState("0");
        int i = employeeDao.insertEmployee(employeeAddParam);
        if (i != 1) {
            throw new CustomerException("5001", "添加失败");
        }
        return i;
    }

    @Override
    public void downloadExcel(HttpServletResponse response) {
        List<EmployeeListVO> list = employeeDao.selectEmployeeExcel();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = "清单数据" + timeStamp;
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        } catch (UnsupportedEncodingException e) {
            throw new CustomerException("8002", "导出失败");
        }
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        try {
            EasyExcel.write(response.getOutputStream(), EmployeeListVO.class).sheet("模板").doWrite(list);
        } catch (IOException e) {
            throw new CustomerException("8002", "导出失败");
        }
    }

    @Override
    public int delEmployeeById(int id) {
        Employee employeeListVO = employeeDao.selectEmployeeById(id);
        if (employeeListVO == null) {
            throw new CustomerException("5002", "数据不存在");
        }
        if (!(employeeListVO.getState().equals("0"))) {
            throw new CustomerException("5003", "只有人事审批可以删除，其余状态无法删除");
        }
        int i = employeeDao.delEmployeeById(id);
        if (i != 1) {
            throw new CustomerException("5001", "删除失败");
        }
        return i;
    }

    @Override
    public int updateSaveEmployee(EmployeeAddOrUpdateParam employeeUpdateParam) {
        if (employeeUpdateParam == null) {
            throw new CustomerException("5002", "传递参数为空");
        }
        if (!(employeeUpdateParam.getState().equals("0"))) {

            employeeUpdateParam.setState(employeeUpdateParam.getState());
        }
        //员工修改数据 状态为0  人事编写
        //employeeUpdateParam.setState("0");
        int i = employeeDao.updateSaveEmployee(employeeUpdateParam);
        return i;
    }

    @Override
    public Employee getEmployeeById(int id) {
        Employee employeeListVO = employeeDao.selectEmployeeById(id);
        if (employeeListVO == null) {
            throw new CustomerException("5002", "数据不存在");
        }
        return employeeListVO;
    }

    @Override
    public int updateState(EmployeeAddOrUpdateParam employeeUpdateParam) {
        Employee employeeListVO = employeeDao.selectEmployeeById(employeeUpdateParam.getId());
        if (employeeListVO == null) {
            throw new CustomerException("5002", "数据不存在");
        }
        if (!(employeeListVO.getState().equals("0"))) {
            throw new CustomerException("5003", "只有[人事审批状态]可以提交，其余状态无法提交");
        }
        employeeUpdateParam.setState("1");

        int i = employeeDao.updateSateEmployeeById(employeeUpdateParam);
        if (i != 1) {
            throw new CustomerException("5001", "更新失败");
        }
        return i;
    }

    @Override
    public PageVO<Employee> getEmployeeStateList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        //String state = "1";
        List<Employee> employeeListVOS = employeeDao.selectEmployeeStateList();
        PageInfo<Employee> employeeListVOPageInfo = new PageInfo<>(employeeListVOS);
        if (employeeListVOPageInfo != null) {
            return new PageVO<>(employeeListVOPageInfo.getTotal(), employeeListVOPageInfo.getList());
        }
        return null;
    }

    @Override
    public int updateStateAgree(Employee employee) {
        employee.setState("2");
        int i = employeeDao.updateSateAgree(employee);
        if (i != 1) {
            throw new CustomerException("5001", "更新失败");
        }
        return i;
    }

    @Override
    public int updateStateNoAgree(Employee employee) {
        employee.setState("3");
        int i = employeeDao.updateSateAgree(employee);
        if (i != 1) {
            throw new CustomerException("5001", "更新失败");
        }
        return 0;
    }


}
